package fpt.edu.vn.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.model.Districts;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class GetDistrictAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String email;
	private List<Districts> lstDistrict;
	private InputStream inputStream;

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public List<Districts> getLstDistrict() {
		return lstDistrict;
	}

	public void setLstDistrict(List<Districts> lstDistrict) {
		this.lstDistrict = lstDistrict;
	}

	private int provinceId;

	private AddnewDao addnewdao = new AddnewDao();
	// For SessionAware
	Map<String, Object> session;

	public String execute() {

		this.lstDistrict = addnewdao.getDistricts(provinceId);

		StringBuilder stbtemp = new StringBuilder();
		for (int i = 0; i < lstDistrict.size(); i++) {
			Districts tempDis = lstDistrict.get(i);
			stbtemp.append(tempDis.getDistrictId() + ","
					+ tempDis.getDistrictName() + "|");
		}
		// put value utf-8 into inputStream
		inputStream = toUTF8InputStream(stbtemp.toString());
		return SUCCESS;

	}

	public static InputStream toUTF8InputStream(String str) {
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// UTF-8 should always be supported
			throw new AssertionError();
		}
		return is;
	}

	public int getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(int provinceId) {
		this.provinceId = provinceId;
	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
